import pygame
from setting import *
import random
vec=pygame.math.Vector2
class Player(pygame.sprite.Sprite):
    def __init__(self,game):
        self.game=game
        #self.groups=self.game.all_sprite
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.image.load('huaqiang.png')
        self.image=pygame.transform.scale(self.image,(150,150))
        self.image.set_colorkey(WHITE)
        #self.image=pygame.image.load(self.standing_frames[0])
        self.image.set_colorkey(LITTLEBLUE)
        self.image=pygame.transform.scale(self.image,(72,100))
        self.rect=self.image.get_rect()
        self.rect.center=(WIDTH/2,HEIGHT/2)
        self.pos=vec(WIDTH/2,HEIGHT/2)
        self.vel=vec(0,0)
        self.acc=vec(0,0)
        self.death=0
        self.height=0
        self.walking=False
        self.jumping=False
        self.current_frame=0
        self.last_update=0
        self.mask=pygame.mask.from_surface(self.image)
    def load_images(self):
        self.standing_frames=['rabit_stoop.png','rabit_stand_still.png']
        self.walk_frames_right=['rabit_right_stand.png','rabit_right_walk.png']
        self.walk_frames_left=['rabit_left_stand.png','rabit_left_walk.png']
        self.walk_frames_list=[]
        '''for frame in self.walk_frames_right:
            self.walk_frames_list.append(pygame.transform.flip(frame,True,False))'''
    def update(self):
        self.acc=vec(0,1)
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            self.acc.x= -PLAYER_ACC
        if keys[pygame.K_RIGHT]:
            self.acc.x = PLAYER_ACC
        #only on the platform can we jump
        #and increase our vel meet the platform
        self.rect.y += 5
        hits = pygame.sprite.spritecollide(self, self.game.platforms, False)
        for i in hits:
            if i.rect.y>hits[0].rect.y:
                hits[0]=i
        self.rect.y -= 5
        if hits:
            if pygame.K_DOWN:
             if keys[pygame.K_UP]:
                     self.vel.y = -23
                     self.game.jump_sound.play()
        if keys[pygame.K_DOWN]:
            self.acc.y = PLAYER_ACC
        self.acc.x+=self.vel.x*PLAYER_FRICTION_X
        self.acc.y+=self.vel.y*PLAYER_FRICTION_Y
        self.vel+=self.acc
        if self.vel.y<-30:
            self.vel.y=-30
        self.pos+=self.vel+self.acc*0.5
        if self.pos.x<0:
            self.pos.x=WIDTH
        if self.pos.x>WIDTH:
            self.pos.x=0
        '''if self.pos.y<0:
            self.pos.y=HEIGHT'''
        if self.pos.y>HEIGHT+50:
            self.death=1
        self.rect.midbottom=self.pos
class Platform(pygame.sprite.Sprite):
    def __init__(self,x,y,game):
        self.game=game
        #self.groups = game.all_sprite, game.platforms
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.image.load('moto_bike.png')
        self.image=pygame.transform.scale(self.image,(200,80))
        self.image.set_colorkey(LITTLEBLUE)
        self.rect=self.image.get_rect()
        self.rect.x=x
        self.rect.y=y
        if 7<random.randrange(1,9):
             self.powerup = Pow(self)
             self.game.all_sprite.add(self.powerup)
             self.game.powerups.add(self.powerup)
        if 7<random.randrange(1,9):
             self.enemy = Enemy(self)
             self.game.all_sprite.add(self.enemy)
             self.game.enemies.add(self.enemy)
'''class Spritesheet:
    #utility class for loading and parsing spritesheets
    def __init__(self,filename):
        self.spritesheet=pygame.image.load(filename)
    def get_image(self,x,y,width,height):
        #grab image out of a larger image spritesheet
        image=pygame.Surface((width,height))
        image.blit(self.spritesheet,(x,y,width,height))
        return image'''
class Pow(pygame.sprite.Sprite):
    def __init__(self,plat):
        #self.groups=self.game.self.all_sprite,self.game.powerups
        pygame.sprite.Sprite.__init__(self)
        self.plat=plat
        self.image=pygame.image.load('water_meleon.png')
        self.image = pygame.transform.scale(self.image, (100, 100))
        self.image.set_colorkey(LITTLEBLUE)
        self.rect=self.image.get_rect()
        self.rect.centerx=self.plat.rect.centerx
        self.rect.bottom=self.plat.rect.top+5
class Enemy(pygame.sprite.Sprite):
    def __init__(self,plat):
        #self.groups=self.game.self.all_sprite,self.game.powerups
        pygame.sprite.Sprite.__init__(self)
        self.plat=plat
        self.image=pygame.image.load('watermelon_saler.png')
        self.image=pygame.transform.scale(self.image,(120,120))
        self.image.set_colorkey((255,255,255))
        self.image.set_colorkey(LITTLEBLUE)
        self.rect=self.image.get_rect()
        self.rect.centerx=self.plat.rect.centerx
        self.rect.bottom=self.plat.rect.top-5
        self.now = pygame.time.get_ticks()
        self.speed_x=5
        self.acc_y=0.5
        self.vel_y=0
        self.pos_y=self.rect.bottom
        self.mask=pygame.mask.from_surface(self.image)
    def update(self):
        self.rect.x+=self.speed_x
        if self.rect.x>WIDTH:
            self.speed_x=-4
        if self.rect.x<0:
            self.speed_x=4
        '''self.vel_y+=self.acc_y
        self.pos_y+=self.vel_y+0.5*self.acc_y
        if self.vel_y<-3:
            self.acc_y=0.5
        if self.vel_y>3:
            self.acc_y=-0.5
        self.rect.bottom=self.pos_y'''
